class TimeUtils {
  // 将时:分:秒.毫秒格式的字符串转换为总毫秒数
  static int formattedTimeToMilliseconds(String formattedTime) {
    // 分离时分秒和毫秒
    List<String> parts = formattedTime.split('.');

    // 获取时分秒部分
    List<String> timeParts = parts[0].split(':');

    // 解析时、分、秒
    int hours = int.parse(timeParts[0]);
    int minutes = int.parse(timeParts[1]);
    int seconds = int.parse(timeParts[2]);

    // 解析毫秒部分，如果没有毫秒，则默认为 0
    int milliseconds = parts.length > 1 ? int.parse(parts[1]) : 0;

    // 计算总毫秒数
    return (hours * 3600 + minutes * 60 + seconds) * 1000 + milliseconds;
  }

  // 将总毫秒数转换为时:分:秒.毫秒格式的字符串
  static String millisecondsToFormattedTime(int totalMilliseconds) {
    int totalSeconds = totalMilliseconds ~/ 1000;
    int milliseconds = totalMilliseconds % 1000;

    int hours = totalSeconds ~/ 3600;
    int minutes = (totalSeconds % 3600) ~/ 60;
    int seconds = totalSeconds % 60;

    // 格式化为 00:00:00.000
    String hoursStr = hours.toString().padLeft(2, '0');
    String minutesStr = minutes.toString().padLeft(2, '0');
    String secondsStr = seconds.toString().padLeft(2, '0');
    String millisecondsStr = milliseconds.toString().padLeft(3, '0');

    return '$hoursStr:$minutesStr:$secondsStr.$millisecondsStr';
  }

  // 将时、分、秒、毫秒转换为总毫秒数
  static int timeToMilliseconds(int hours, int minutes, int seconds, int milliseconds) {
    return (hours * 3600 + minutes * 60 + seconds) * 1000 + milliseconds;
  }

  // 将总毫秒转换为一个包含时、分、秒、毫秒的Map
  static Map<String, int> millisecondsToTimeComponents(int totalMilliseconds) {
    int hours = totalMilliseconds ~/ 3600000;
    int minutes = (totalMilliseconds % 3600000) ~/ 60000;
    int seconds = (totalMilliseconds % 60000) ~/ 1000;
    int milliseconds = totalMilliseconds % 1000;

    return {
      'hours': hours,
      'minutes': minutes,
      'seconds': seconds,
      'milliseconds': milliseconds,
    };
  }
}
